import dayjs from 'dayjs'
export function formatDate(date, format = 'YYYY-MM-DD') {
    return date && dayjs(date).format(format)
}

export function padLeftZero(str) {
    console.log('444 :>> ', 444)
    return ('00' + str).substr(str.length)
}

/** ------------------------------------------------------------------
 * 把数组里对象的id值转换为name
 * @param {Number} value 存在两种格式：单个的是number，多个是逗号分割的String
 * @param {Array} arr
 * @param {any} key被转换的对象
 * @param {any} name转换成的对象
 * arr默认数据类型如下
 * [{
    name: '未知',
    id: 0
  }, {
    name: '男',
    id: 1
  }, {
    name: '女',
    id: 2
  }]
 */
export function value2Name(value, arr, key, name) {
    if (!toString(value)) return ''
    const k = key || 'id'
    const v = name || 'name'
    if (typeof value === 'number') {
        if (Array.isArray(arr)) {
            for (let i = 0; i < arr.length; i++) {
                if (arr[i][k] === value) {
                    return arr[i][v]
                }
            }
        }
    } else if (typeof value === 'string') {
        if (Array.isArray(arr)) {
            const valueList = value.split(',').map(Number)
            const nameList = []
            for (const item of valueList) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i][k] === item) {
                        nameList.push(arr[i][v])
                        break
                    }
                }
            }
            return nameList.toString()
        }
    }
}
